# -*- coding: utf-8 -*-

from dbobject import DBObject
from aeConn import dbHandle
import reaction
import compound

import yaml

class Pathway(DBObject):
	def __init__(self, key=''):
		"""key is either the key or another Pathway
		"""
		if (type(key)==Pathway):
			DBObject.__init__(self, key)
			return
		else:
			DBObject.__init__(self)

		self.key = key

		#dict of items we have (overriding __dict__)
		# This is the graph of the pathway the form is:
		# {Reaction1:(Reaction2,Compound)} which means
		# Reaction1 points to Reaction2 via Compound
		self._things['graph'] = {}

		self._things['name'] = ""


	def __iter__(self):
		for key in self._things['graph']:
			yield key, self._things['graph'][key][0], self._things['graph'][key][1]


	def _loadYAML(self, yml):
		data = yaml.load(yml)
		graph = {}
		if "graph" in data.keys():
			for key in data['graph'].keys():
				graph[Reaction(key)] = (Reaction(data["graph"][key][0]), Compound(data["graph"][key][1]))
			del data["graph"]

		DBObject._loadYAML(self, data)

	def _dumpYAML(self, properties=[]):
		if properties:
			keys = properties
		else:
			keys = self.properties().keys()

		data = {}

		if "graph" in keys:
			keys.remove("graph")
			g = {}
			for reaction in self._things['graph']:
				g[reaction.key] = (self._things['graph'][reaction][0].key, self._things['graph'][reaction][1].key)
			data["graph"] = g

		rest = list(set(keys) - set(data.keys()))
		return DBObject._dumpYAML(self, otherData=data, properties=rest)